#include "RGBInputFilter.h"
#include <QDebug>
#include <memory>
#include <algorithm>

RGBInputFilter::RGBInputFilter(int width, int height, int fps, bool mirror) : width(width), height(height), InputFilter()
{
	mapMode.nXRes = width;
	mapMode.nYRes = height;
	mapMode.nFPS = fps;

	XnStatus status = image.Create(context);
	image.SetMapOutputMode(mapMode);
	image.GetMirrorCap().SetMirror(mirror);

	outputs[0] = cv::Mat(cv::Size(width, height), CV_8UC3);

	context.StartGeneratingAll();
}


RGBInputFilter::~RGBInputFilter(void)
{
	image.Release();
}

void RGBInputFilter::Process()
{
	std::memcpy(outputs[0].data, image.GetRGB24ImageMap(), width*height*3);

	//swap R with B	
	uchar *r = outputs[0].data;
	uchar *b = outputs[0].data + 2;

	uchar *end = r + width*height*3;
	while (r != end)
	{
		std::swap(r,b);
		r += 3;
		b += 3;
	}
}